package controller

import (
	"example.com/myproject/project/global"
	"example.com/myproject/project/models"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

/*
	订票  航班，身份证，联系方式 + 用户id(现在方便测试直接传id)
	{
		"Userid": 1,
		"FlightNumber": 20241130,
		"Idcard": "114514",
		"PNumber": "123456789"
	}
		//用户id从登录的拦截器获得，这种请求头会有一个JWT,从中获取id
		if username, exists := c.Get("username"); exists {
			c.JSON(http.StatusOK, gin.H{"username": username})
		} else {
			c.JSON(http.StatusNotFound, gin.H{"message": "Username not found"})
		}
*/

func OrderTicket(c *gin.Context) {
	var input struct {
		Userid       int    `json:"Userid"`
		FlightNumber int    `json:"FlightNumber"`
		Idcard       string `json:"Idcard"`
		PNumber      string `json:"PNumber"`
	}
	if err := c.ShouldBindJSON(&input); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
		return
	}

	//创建订单
	var order models.Order
	order.UserID = input.Userid
	order.FlightID = input.FlightNumber
	if err := global.Db.Create(&order).Error; err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
		return
	}
	//创建乘客表
	var passenger models.Passenger
	passenger.FlightNumber = input.FlightNumber
	passenger.UserID = input.Userid
	passenger.IdentityCard = input.Idcard
	passenger.ContactInfo = input.PNumber
	if err := global.Db.Create(&passenger).Error; err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
		return
	}
	//座位减一
	var tmpaircraft models.Aircraft
	global.Db.Preload("Flight").Where("current_flight_number = ?", input.FlightNumber).First(&tmpaircraft)
	if tmpaircraft.SeatCount > 0 {
		global.Db.Model(&tmpaircraft).Update("seat_count", tmpaircraft.SeatCount-1)
	} else {
		c.JSON(200, gin.H{"error": "no more seat"})
	}
	c.JSON(http.StatusOK, gin.H{"error": 0, "orderid": order.OrderID})
}

/*
	取消: 订单 + 用户id(现在方便测试直接传id)
	{
		"Userid": 1,
		"Orderid": 5,
	}

删订单表，乘客表，对应座位加一
*/
func CancelOrder(c *gin.Context) {
	var input struct {
		Userid  int `json:"Userid"`
		Orderid int `json:"Orderid"`
	}
	if err := c.ShouldBindJSON(&input); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"err": err})
		return
	}
	var order models.Order
	order.OrderID = input.Orderid
	order.UserID = input.Userid
	if err := global.Db.Where("user_id = ?", input.Userid).First(&order).Error; err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"err": err})
		return
	}

	if err := global.Db.Delete(&order).Error; err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"err": err})
		return
	}
	fmt.Println("order infomation:", order)
	var passenger models.Passenger
	passenger.FlightNumber = order.FlightID
	passenger.UserID = input.Userid
	if err := global.Db.Delete(&passenger).Error; err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"err": err})
		return
	}
	var tmpaircraft models.Aircraft
	global.Db.Preload("Flight").Where("current_flight_number = ?", order.FlightID).First(&tmpaircraft)
	global.Db.Model(&tmpaircraft).Update("seat_count", tmpaircraft.SeatCount+1)
	c.JSON(http.StatusOK, gin.H{"err": "0"})
}
